﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using System;
using OI.DataModel;
using System.Linq;

#if SILVERLIGHT && !WINDOWS_PHONE
using Caliburn.Micro;
using OI.Framework;
using System.Windows.Browser;
#endif

namespace OI
{
    public static class Configuration
    {
        // these are loaded via Silverlight init params
        public static string ConnectionString;
        public static string BingServiceKey;
        
        public static readonly string MDNSEndpoint = "MDNUXServices/";
        public static readonly string RESTEndpoint = "REST/9.0/Map/";
        public static readonly string OGCEndpoint = "OGC/";
        public static readonly string RSSEndpoint = "RSSFeed";
        public static readonly string ODATAEndpoint = "https://odata.sqlazurelabs.com/OData.svc/v0.1/wirnav97cn/OI/";
        public static readonly string CatalogServiceEndpoint = "DataServices/CatalogService.svc";
        public static readonly string DataServiceEndpoint = "DataServices/DataPackagingService.svc";
        public static readonly string SharingServiceEndpoint = "DataServices/SharingService.svc";
        public static readonly string StartPage = "OI.aspx";
        public static readonly string DefaultThumbnailPath = "/Images/AppImages/";
        public static readonly string DefaultThumbnailFile = "AppThumbnailDefault.png";
        public static readonly string FiltersMetaKey = "Filters";
        public static readonly string DensityMappingMetaKey = "DensityMapping";
        public static readonly string DynamicLabelingMetaKey = "DynamicLabeling";
        public static readonly string DrilldownHeadersMetaKey = "DrilldownHeaders";
        public static readonly string DrilldownColumnsMetaKey = "DrilldownColumns";
        public static readonly string DrilldownPhotoColumnMetaKey = "DrilldownPhotoColumn";
        public static readonly string DrilldownIgnoreMetaKey = "DrilldownIgnore";
        public static readonly string SearchColumnsMetaKey = "SearchColumns";
        public static readonly string HyperlinkColumnsMetaKey = "HyperlinkColumns";
        public static readonly string LayerNameMetaKey = "LayerName";
        public static readonly string HideLayersOutOfScale = "HideLayersOutOfScale";
        public static readonly string RSSEnableMetaKey = "RSSEnable";
        public static readonly string SuppressDataDownloadMetaKey = "SuppressDataDownload";
        public static readonly string EnableMouseHoverEventsMetaKey = "EnableMouseHoverEvents";
        public static readonly string QueryStringJurisdictionID = "jurisdictionID";
        public static readonly string QueryStringShareID = "shareID";
        public static readonly string QueryStringFilterLayerID = "filterLayerID";
        public static readonly string QueryStringFilterMetadata = "filterMetadata";
        public static readonly string QueryStringDebug = "debug";
        public static readonly int BingMapsGeneration = 516;
        public static readonly int DataDetailNameMaxStringLength = 50;
        public static readonly int LegendIconSize = 26;
        public static readonly int MaxSpatialQueryRows = 500;
        public static readonly int MaxSearchQueryRows = 5;
        public static readonly int MaxDrillDownQueryRows = 5;
        public static readonly int MaxSampleQueryRows = 15;
        public static readonly int DrillDownPixelRadius = 12;
        public static readonly double MaxZoomToShapeZoomLevel = 17;
        public static double ShapeOutlineThickness = 5;
        public static double PointMarkerSize = 12;

        // jurisdiction specific
        public static OIJurisdiction Jurisdiction
        {
            get 
            {
                return _Jurisdiction; 
            }
            set 
            { 
                _Jurisdiction = value; 
            }
        }
        private static OIJurisdiction _Jurisdiction = new OIJurisdiction();

        // use this ID if none is passed on the querystring
        public static readonly string DefaultJurisdictionID = "sf.gov";

#if SILVERLIGHT && !WINDOWS_PHONE
        // will return the web host URI base
        public static string GetBaseURI()
        {
            var current = System.Windows.Application.Current;
            if (current != null)
            {
                var src = current.Host.Source;
                if (src != null)
                {
                    string uri = src.AbsoluteUri;
                    int idx = uri.ToLower().IndexOf("/clientbin");
                    if (idx > 0)
                    {
                        return uri.Substring(0, idx + 1);
                    }
                }
                
            }
            return "http://localhost/";
        }


        // fetch the jurisdiction id off of the querystring
        public static string GetQueryStringJurisdictionID()
        {
            return HtmlPage.Document.QueryString.ContainsKey(QueryStringJurisdictionID) ?
                HtmlPage.Document.QueryString[QueryStringJurisdictionID] :
                DefaultJurisdictionID;
        }

        // fetch the share id off of the querystring
        public static string GetQueryStringShareID()
        {
            return HtmlPage.Document.QueryString.ContainsKey(QueryStringShareID) ?
                HtmlPage.Document.QueryString[QueryStringShareID] : 
                null;
        }


        // fetch the filter layer of the querystring
        public static string GetQueryStringFilterLayerID()
        {
            return HtmlPage.Document.QueryString.ContainsKey(QueryStringFilterLayerID) ?
                HtmlPage.Document.QueryString[QueryStringFilterLayerID] :
                null;
        }

        // fetch the filter layer of the querystring
        public static bool GetDebug()
        {
            return HtmlPage.Document.QueryString.ContainsKey(QueryStringDebug) 
                ? !(new[] { "0", "false", "no" }).Contains(HtmlPage.Document.QueryString[QueryStringDebug], StringComparer.OrdinalIgnoreCase)
                : false;
        }


        // fetch the filter off of the querystring
        public static string GetQueryStringFilterMetadata()
        {
            if (HtmlPage.Document.QueryString.ContainsKey(QueryStringFilterMetadata))
            {
                string f = HtmlPage.Document.QueryString[QueryStringFilterMetadata];
                return HttpUtility.UrlDecode(f);
            }
            return null;
        }
#endif
    }
}
